var _bFieldSelectPrimaryField = false;

var _iFieldSelectHeight		= 200;
var _sFieldSelectLeftTitle	=  LOCID_TOOLS_AVAILABLE_FIELDS;
var _sFieldSelectRightTitle	=  LOCID_TOOLS_FIELDS_RESULTS;
var _bFieldSelectShowUp		= true;
var _bFieldSelectShowDown	= true;

var _iUsedAry;



function drawFieldSelect(sAvailAry, sSelectedAry)
{
divFieldSelect.innerHTML = "<table cellspacing='5' cellpadding='0' style='table-layout:fixed;'><col width='*'><col width='50px'><col width='*'><tr><td><b class='label'>" + _sFieldSelectLeftTitle + "</b></td><td>&nbsp;</td><td><b class='label'>" + _sFieldSelectRightTitle + "</b></td></tr><tr><td><div id='objList' class='divList' style='width:100%;height:" + _iFieldSelectHeight + "px;' tabIndex=0 onfocus='handleListFocus()' onkeydown='handleListKey()' hidefocus='true'></div></td><td align='center'><button id='btnRight' style='width: 40px; align: 'center' onclick='MoveRight();'>>></button><p><button id='btnLeft' style='width: 40px;' onclick='MoveLeft();'><<</button>" + ((_bFieldSelectShowUp) ? "<p><button id='btnUp' style='width: 40px;' onclick='MoveUp();'>Up</button>" : "") + ((_bFieldSelectShowDown) ? "<p><button id='btnDown' style='width: 40px;' onclick='MoveDown();'>Down</button>" : "") + "</td><td><div id='rtnObjList' class='divList' style='width:100%;height:" + _iFieldSelectHeight + "px;' tabIndex=0 onfocus='handleListFocus()' onkeydown='handleListKey()' hidefocus='true'></div></td></tr></table>";

objList.innerHTML = "<table cellpadding='2' cellspacing='0' width='100%'></table>";

var iAvailLen = sAvailAry[0].length;

for (var i = 0; i < iAvailLen; i++)
{
CreateNewRow(i, sAvailAry[0][i], sAvailAry[0][i], sAvailAry[1][i], objList, false);
}





rtnObjList.innerHTML = "<table cellpadding='2' cellspacing='0' width='100%'></table>";

var iSelectedLen;
var bFound;

if (sSelectedAry)
{
iSelectedLen = sSelectedAry[0].length;
}
else
{
iSelectedLen = 0;
}



_iUsedAry = new Array(iAvailLen);
for (var i = 0; i < iAvailLen; i++)
{
_iUsedAry[i] = 0;
}

for ( var j= 0; j < iSelectedLen; j++)
{
bFound = false;

for ( i = 0; i < iAvailLen; i++)
{
if ( sAvailAry[0][i] == sSelectedAry[0][j])
{
var iOffset = 0;
for ( var k = 0; k < i; k++)
{
iOffset += _iUsedAry[k];
}

objList.firstChild.rows[i-iOffset].selected = 1;
MoveRight();
bFound = true;
break;
}
}

if ( (0 == j) && _bFieldSelectPrimaryField )
{
rtnObjList.firstChild.rows[0].disabled = true;
}

if (bFound != true)
{


break;
}
}




if (_bFieldSelectPrimaryField && (0 == iSelectedLen))
{
alert( LOCID_CANT_MOVE_PRIMARY_FIELD );
}
}


function handleListFocus()
{
var oTable = event.srcElement.firstChild;
if(IsNull(oTable.lastSelected) && oTable.rows.length > 0)
{
oTable.rows[0].click();
}
}


function handleListKey()
{
var o = event.srcElement;
while (o.tagName != "DIV")
{
o = o.parentElement;
}
oTable = o.firstChild;
o = oTable.lastSelected;
switch(event.keyCode)
{
case 40:
if(o && o.nextSibling)
{

if(event.ctrlKey && o.nextSibling.selected==1)
{
o.click();
oTable.lastSelected = o.nextSibling;
o.nextSibling.firstChild.className = "currSel";
}
else
{
o.nextSibling.click();
}
oTable.lastSelected.scrollIntoView(false);
event.cancelBubble = true;
event.returnValue = false;
}
break;

case 38:
if(o && o.previousSibling)
{
if(event.ctrlKey && o.previousSibling.selected==1)
{
o.click();
oTable.lastSelected = o.previousSibling;
o.previousSibling.firstChild.className = "currSel";
}
else
{
o.previousSibling.click();
}
oTable.lastSelected.scrollIntoView(false);
event.cancelBubble = true;
event.returnValue = false;
}
break;

case 32:
if(o)
{

var i = o.rowIndex + 1, oSel = null;
while(i<oTable.rows.length)
{
if(oTable.rows[i].selected == 0)
{
oSel = oTable.rows[i];
break;
}
i++;
}
i = o.rowIndex - 1;
while(!oSel && i >= 0)
{
if(oTable.rows[i].selected == 0)
{
oSel = oTable.rows[i];
break;
}
i--;
}

o.fireEvent("ondblclick", event);
if(oSel)
{
oSel.click();
}
event.cancelBubble = true;
event.returnValue = false;
}
break;
}
}









function CreateNewRow(iIndex, sId , sName, sDisplay, oDestList, bReinsert)
{
var oTable	= oDestList.firstChild;
var oRow;
var oCell;




if ( bReinsert)
{

var iLenDestList	= _iUsedAry.length;
var iNewLoc			= 0;


for (var i = iIndex; -1 < i; i--)
{
var iOffset = 0;
for ( var k = 0; k < i; k++)
{
iOffset += _iUsedAry[k];
}
_iUsedAry[i] = 0;
var iNewIndex = i - iOffset;

if ( (0 == iNewIndex) || (parseInt(oTable.rows[iNewIndex-1].cells[0].getAttribute("index"),10) < iIndex))
{
iNewLoc = parseInt(iNewIndex,10);
break;
}
}
oRow		= oTable.insertRow( [iNewLoc] );
oCell		= oRow.insertCell();
}
else
{
if ((oDestList == rtnObjList) ||(-1 == iIndex))
{
oRow				= oTable.insertRow();
_iUsedAry[iIndex]	= 1;
}
else
{
oRow		= oTable.insertRow( [iIndex] );
}

oCell				= oRow.insertCell();
}

oCell.id = sId ;
oCell.index = iIndex ;

oCell.className	= "sel";
oCell.noWrap	= true;
oCell.innerHTML	= "<nobr class='sel-nobr'>" + CrmEncodeDecode.CrmHtmlEncode(sDisplay) + "</nobr>";

oRow.name = sName;
oRow.selected = 0;
oRow.attachEvent("onclick", selectRow);
oRow.attachEvent("ondblclick", MoveToOtherList);

}

function GetListName()
{
var o	= event.srcElement;
var oId	= o.getAttribute('id');

while ( (oId != 'objList') && (oId != 'rtnObjList') )
{
o = o.parentElement;
oId = o.getAttribute('id');
}

return oId;
}

function MoveToOtherList()
{

if ( GetListName() == 'objList' )
{
MoveRight();
}
else
{
MoveLeft();
}

}

function MoveLeft()
{
var oRows	= rtnObjList.firstChild.rows;
var iLen	= oRows.length;

for (var i = 0; i < iLen; i++)
{
if (1 == oRows[i].selected)
{
CreateNewRow(oRows[i].cells[0].getAttribute("index"), oRows[i].cells[0].getAttribute("id") , oRows[i].name, oRows[i].innerText, objList, true);


rtnObjList.firstChild.deleteRow(i);

i--;
iLen--;
rtnObjList.firstChild.lastSelected = null;
}
}


unSelectRows(rtnObjList);

}

function MoveRight()
{
var oRows	= objList.firstChild.rows;
var iLen	= oRows.length;

for (var i = 0; i < iLen; i++)
{

if (1 == oRows[i].selected)
{
CreateNewRow(oRows[i].cells[0].getAttribute("index"),oRows[i].cells[0].getAttribute("id"), oRows[i].name, oRows[i].innerText, rtnObjList, false);


objList.firstChild.deleteRow(i);
i--;
iLen--;
objList.firstChild.lastSelected = null;
}
}
}


function unSelectRows(oDiv)
{
var oRows	= oDiv.firstChild.rows;
var iLen	= oRows.length;

for (var i = 0; i < iLen; i++)
{
oRows[i].selected = 0;
oRows[i].runtimeStyle.backgroundColor = "#ffffff";
}
}


function selectRow()
{
var o = event.srcElement;

while (o.tagName != "TR")
{
o = o.parentElement;
}


var bIsRtnObjList = ( 'rtnObjList' == GetListName());
var oTable = bIsRtnObjList ? rtnObjList.firstChild : objList.firstChild;



if (event.ctrlKey || event.shiftKey)
{
if (  bIsRtnObjList && (true == IsMultiSelection(rtnObjList)) )
{
if (_bFieldSelectShowUp)
{
document.all("btnUp").disabled		= true;
}
if (_bFieldSelectShowDown)
{
document.all("btnDown").disabled	= true;
}
}
}
else
{
unSelectRows(o.parentElement.parentElement.parentElement);

if (_bFieldSelectShowUp)
{
document.all("btnUp").disabled		= false;
}
if (_bFieldSelectShowDown)
{
document.all("btnDown").disabled	= false;
}
}

if(event.shiftKey && oTable.lastSelected)
{
i = Math.min(oTable.lastSelected.rowIndex, o.rowIndex);
for(;i<=Math.max(oTable.lastSelected.rowIndex, o.rowIndex); i++)
{
oTable.rows[i].selected = 1;
oTable.rows[i].runtimeStyle.backgroundColor = "#c4ddff";
}
}
else
{
o.selected = (o.selected==1) ? 0 : 1;
o.runtimeStyle.backgroundColor = (o.selected==1) ? "#c4ddff" : "";
}

var oTable = o;
while(oTable.tagName != "TABLE")
{
oTable = oTable.parentElement;
}
if(oTable.lastSelected)
{
oTable.lastSelected.firstChild.className = "sel";
}
o.firstChild.className = o.selected==1 ? "currSel" : "sel";
oTable.lastSelected = o;
}




function IsMultiSelection(oList)
{
var oTable	= oList.firstChild;
var oRows	= oTable.rows;
var iLen	= oRows.length;
var bMulti	= false;

for ( var i = 1; i < iLen; i++ )
{
if (true == oRows[i].selected)
{
bMulti = true;
break;
}
}

return bMulti;
}





function MoveUp()
{
var oTable		= rtnObjList.firstChild;
var oRows		= oTable.rows;
var iLen		= oRows.length;
var iFirstIndex = ((true == _bFieldSelectPrimaryField) ? 2 : 1)

for ( var i = iFirstIndex; i < iLen; i++ )
{
if (1 == oRows[i].selected)
{
oTable.moveRow( i, i - 1 );
break;
}
}
}



function MoveDown()
{
var oTable	= rtnObjList.firstChild;
var oRows	= oTable.rows;
var iLen	= oRows.length;

for ( var i = (iLen-2); 0 <= i; i-- )
{
if (1 == oRows[i].selected)
{
oTable.moveRow( i, i + 1 );
break;
}
}
}



function GetReturnList()
{

var oRows		= rtnObjList.firstChild.rows;
var iLen		= oRows.length;

var sRetAry		= new Array(iLen);

for (var i = 0; i < iLen; i++)
{
sRetAry[i] = oRows[i].name;
}

return sRetAry;

}




function Partition(A1, p, r, A2)
{
var x = A1[p];
var i = p;
var j = r;

while (true)
{
while ( A1[j].localeCompare(x) > 0 )
{
j--;
}
while ( A1[i].localeCompare(x)<0 )
{
i++;
}

if ( i < j )
{

if(x.localeCompare(A1[j])==0)
{
j--;
}
else
{

var tmp = A1[i];
A1[i] = A1[j];
A1[j] = tmp;


tmp = A2[i];
A2[i] = A2[j];
A2[j] = tmp;
}
}
else
{
return j;
}
}
}











function Quicksort(A1, p, r, A2)
{
var q;

if (p < r)
{
q = Partition(A1, p, r, A2);
Quicksort(A1, p, q, A2);
Quicksort(A1, q + 1, r, A2);
}
}

